package com.atguigu.eduservice.service.impl;

import com.atguigu.commonutils.vo.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.eduservice.vo.TeacherQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-03-18
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService
{
    @Resource
    private EduTeacherMapper eduTeacherMapper;

    /**
     *  多条件组合查询讲师
     */
    @Override
    public R pageTeacherCondition(Long current, Long limit, TeacherQuery teacherQuery)
    {
        //创建page对象
        Page<EduTeacher> page =  new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        //多条件组合查询
        //判断条件值是否为空，如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(StringUtils.isNotEmpty(name))
        {
            queryWrapper.like("name",name);
        }
        if(level != null)
        {
            queryWrapper.eq("level",level);
        }
        if(StringUtils.isNotEmpty(begin))
        {
            //  g greater   e equal    l less    t than
            queryWrapper.ge("gmt_create",begin);
        }
        if(StringUtils.isNotEmpty(end))
        {
            //  g greater   e equal    l less    t than
            queryWrapper.le("gmt_modified",end);
        }
        //调用方法实现分页查询
        eduTeacherMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return R.success().data("total",total).data("rows",records);
    }


    /**
     *
     /**
     * 分页查询讲师
     * @param current
     * @param limit
     * @return
     */
    @Override
    public R pageListTeacher(Long current, Long limit)
    {
        Page<EduTeacher> page = new Page<>(current, limit);
        //调用该方法时，会把分页所有数据封装到page里面
        eduTeacherMapper.selectPage(page,null);

        long total = page.getTotal();//前端需要的总记录数
        List<EduTeacher> records = page.getRecords();//数据
        return R.success().data("total",total).data("rows",records);
    }

    /**
     * 查询讲师所有数据
     * @return
     */
    @Override
    public R findAll()
    {
        return R.success().data("items",eduTeacherMapper.selectList(null));
    }

    /**
     * 通过讲师id逻辑删除讲师
     * @param id
     * @return
     */
    public R removeById(String id)
    {
        eduTeacherMapper.deleteById(id);
        return R.success();
    }

    /**
     * 添加讲师
     */
    @Override
    public R addTeacher(EduTeacher eduTeacher)
    {
        int insert = eduTeacherMapper.insert(eduTeacher);
        if(insert >= 1)
        {
            return R.success();
        }else
        return R.fail();
    }
}
